package com.yidu.common.utils;

import com.yidu.common.annotation.EqualQueryField;
import com.yidu.common.domain.BaseEntity;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class EntityUtil
{
    public static Map<String, Object> getMapByEntity(BaseEntity entity)
    {
        Map<String, Object> result = new HashMap<>();
        if (entity == null) return result;
        Class<? extends BaseEntity> clazz = entity.getClass();
        for (Field field : clazz.getDeclaredFields())
        {
            try
            {
                if (field.getAnnotation(EqualQueryField.class) != null)
                {
                    field.setAccessible(true);
                    Object value = field.get(entity);
                    if (value != null)
                    {
                        result.put(field.getName(), field.get(entity));
                    }
                }
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}
